package com.dwxt.cdms.applets.service;

import com.dwxt.cdms.applets.dto.IndexResultDTO;
import com.dwxt.cdms.base.Page;
import com.dwxt.cdms.constant.AppletsConstant;
import com.dwxt.cdms.constant.EquipCategory;
import com.dwxt.cdms.constant.ReceiveDataType;
import com.dwxt.cdms.dao.*;
import com.dwxt.cdms.dao.oufu.OuFuDataMapper;
import com.dwxt.cdms.dao.yumeng.*;
import com.dwxt.cdms.entity.*;
import com.dwxt.cdms.entity.oufu.OuFuData;
import com.dwxt.cdms.entity.poct.Poct4;
import com.dwxt.cdms.entity.poct.Poct8;
import com.dwxt.cdms.entity.vo.YouthHopeVO;
import com.dwxt.cdms.entity.witheart.Report;
import com.dwxt.cdms.service.*;
import com.dwxt.cdms.util.DataUtil;
import com.dwxt.cdms.util.ListUtil;
import com.dwxt.cdms.util.RunUtils;
import com.dwxt.cdms.view.wristwatch.NewSleepDataView;
import com.dwxt.common.base.BaseResult;
import com.dwxt.common.util.DateUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.text.ParseException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class HistoryService {
    @Autowired
    private HeartVoiceMapper heartVoiceMapper;

    @Autowired
    private BlutdruckMapper blutdruckMapper;

    @Autowired
    private BloodSugarMapper bloodSugarMapper;

    @Autowired
    private StepDataMapper stepDataMapper;

    @Autowired
    private SleepDataMapper sleepDataMapper;

    @Autowired
    private HrDataMapper hrDataMapper;

    @Autowired
    private BpDataMapper bpDataMapper;

    @Autowired
    private BtDataMapper btDataMapper;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private OuFuService ouFuService;

    @Autowired
    private PoctService poctService;

    @Autowired
    private EquipmentCustomService equipmentCustomService;

    @Autowired
    private YouthHopeService youthHopeService;

    @Autowired
    private LbWatchMapper lbWatchMapper;

    @Autowired
    private LbWatchDataMapper lbWatchDataMapper;
    @Autowired
    private LbWatchSleepMapper lbWatchSleepMapper;
    @Autowired
    private LbWatchStepMapper lbWatchStepMapper;
    @Autowired
    private OuFuDataMapper ouFuDataMapper;

    public BaseResult allHistory(Integer customerId) {
        //心电仪、血压仪、血糖仪
        HeartVoice heartVoice = selectLatestHeartVoiceHistory(customerId);
        Blutdruck blutdruck = (Blutdruck) selectLatestDataHistory(customerId, Blutdruck.class, blutdruckMapper);
        BloodSugar bloodSugar = (BloodSugar) selectLatestDataHistory(customerId, BloodSugar.class, bloodSugarMapper);
        if (bloodSugar != null) {
            bloodSugar.setBloodSugar(strToDouble(bloodSugar.getBloodSugar()));
        }

        OuFuData stepOuFuData = new OuFuData();
        OuFuData hrOuFuData = new OuFuData();
        OuFuData bpOuFuData = new OuFuData();
        OuFuData btOuFuData = new OuFuData();
        OuFuData boOuFuData = new OuFuData();
        NewSleepDataView newSleepDataView = new NewSleepDataView();
        String equipmentCategory = "";
        List<Equipment> equipmentList = equipmentCustomService.selectEquipmentByCustomerId(customerId);
        if (equipmentList.stream().allMatch(equipment -> equipment.getCategory().equals(EquipCategory.LB))) {
            //来邦
            equipmentCategory = EquipCategory.LB;
            LbWatchStep step = lbWatchStepMapper.selectLastByCustomerId(customerId);
            if (step != null) {
                stepOuFuData.setCustomerId(customerId);
                stepOuFuData.setType(ReceiveDataType.STEP);
                stepOuFuData.setSteps(step.getStepsNumber());
                stepOuFuData.setCreateTime(step.getCreateTime());
            }
            LbWatchData lbWatchData = lbWatchDataMapper.selectLastByCustomerId(customerId);
            if (lbWatchData != null) {
                //心率
                hrOuFuData.setCustomerId(customerId);
                hrOuFuData.setType(ReceiveDataType.HEARTBEAT);
                hrOuFuData.setHeartbeat(lbWatchData.getHeartRate());
                hrOuFuData.setCreateTime(lbWatchData.getCreateTime());
                // 血压
                bpOuFuData.setCustomerId(customerId);
                bpOuFuData.setType(ReceiveDataType.BLOOD_PRESSURE);
                bpOuFuData.setDiastolic(lbWatchData.getDiastolicPressure());
                bpOuFuData.setShrink(lbWatchData.getSystolicPressure());
                bpOuFuData.setCreateTime(lbWatchData.getCreateTime());
                // 血氧
                boOuFuData.setCustomerId(customerId);
                boOuFuData.setType(ReceiveDataType.BLOOD_OXYGEN);
                boOuFuData.setBloodOxygen(lbWatchData.getBloodOxygen());
                boOuFuData.setCreateTime(lbWatchData.getCreateTime());
            }
            LbWatchSleep lbWatchSleep = lbWatchSleepMapper.selectLastByCustomerId(customerId);
            if (lbWatchSleep != null) {
                newSleepDataView.setSleepingTime1(String.valueOf(lbWatchSleep.getDeepSleep()));
                newSleepDataView.setSleepingTime2(String.valueOf(lbWatchSleep.getShallowSleep()));
                newSleepDataView.setSleepingTime3(String.valueOf(lbWatchSleep.getSoberSleep()));
                ZoneId zoneId = ZoneId.systemDefault();
                Instant instant = lbWatchSleep.getCreateTime().toInstant();
                LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
                newSleepDataView.setCreateTime(localDateTime);
            }
        } else if (equipmentList.stream().allMatch(equipment -> equipment.getCategory().equals(EquipCategory.ZNSH))) {
            //oufu
            equipmentCategory = EquipCategory.ZNSH;
            stepOuFuData = ouFuService.selectLastDataByType(customerId, ReceiveDataType.STEP);
            newSleepDataView = ouFuService.getNewSleepDataView(customerId);
            if (newSleepDataView != null) {
                newSleepDataView.setType(ReceiveDataType.SLEEP);
            }
            hrOuFuData = ouFuService.selectLastDataByType(customerId, ReceiveDataType.HEARTBEAT);
            bpOuFuData = ouFuService.selectLastDataByType(customerId, ReceiveDataType.BLOOD_PRESSURE);
            btOuFuData = ouFuService.selectLastDataByType(customerId, ReceiveDataType.DOUBLE_TEMPERATURE);
        }
        Customer customer = customerService.selectCustomerById(customerId);
        List<Object> list = new ArrayList<>();

        list.add(getMap("心电仪", heartVoice));
        list.add(getMap("血压仪", blutdruck));
        list.add(getMap("血糖仪", bloodSugar));

        list.add(getMap("计步", equipmentCategory, stepOuFuData));
        list.add(getMap("睡眠", equipmentCategory, newSleepDataView));
        list.add(getMap("心率", equipmentCategory, hrOuFuData));
        list.add(getMap("血压", equipmentCategory, bpOuFuData));
        list.add(getMap("体温", equipmentCategory, btOuFuData));
        list.add(getMap("健康报告", customer));
        return BaseResult.ok(list);
    }

    public BaseResult historyPage(Page page, Integer customerId, String type, String equipmentCategory) {
        List<OuFuData> list = new ArrayList<>();
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        if (EquipCategory.LB.equals(equipmentCategory)) {
            if (ReceiveDataType.STEP.equals(type)) {
                //步数
                List<LbWatchStep> stepList = lbWatchStepMapper.selectPageByCustomerId(customerId);
                list = stepList.stream().map(lbWatchStep -> {
                    OuFuData ouFuData = new OuFuData();
                    ouFuData.setSteps(lbWatchStep.getStepsNumber());
                    ouFuData.setType(ReceiveDataType.STEP);
                    ouFuData.setCustomerId(customerId);
                    ouFuData.setCreateTime(lbWatchStep.getCreateTime());
                    return ouFuData;
                }).collect(Collectors.toList());
            }

            List<LbWatchData> lbWatchDataList = lbWatchDataMapper.selectPageByCustomerId(customerId);
            if (ReceiveDataType.BLOOD_PRESSURE.equals(type)) {
                //血压
                list = lbWatchDataList.stream().map(lbWatchData -> {
                    OuFuData ouFuData = new OuFuData();
                    ouFuData.setDiastolic(lbWatchData.getDiastolicPressure());
                    ouFuData.setShrink(lbWatchData.getSystolicPressure());
                    ouFuData.setType(ReceiveDataType.BLOOD_PRESSURE);
                    ouFuData.setCustomerId(customerId);
                    ouFuData.setCreateTime(lbWatchData.getCreateTime());
                    return ouFuData;
                }).collect(Collectors.toList());
            } else if (ReceiveDataType.HEARTBEAT.equals(type)) {
                //心率
                list = lbWatchDataList.stream().map(lbWatchData -> {
                    OuFuData ouFuData = new OuFuData();
                    ouFuData.setHeartbeat(lbWatchData.getHeartRate());
                    ouFuData.setType(ReceiveDataType.HEARTBEAT);
                    ouFuData.setCustomerId(customerId);
                    ouFuData.setCreateTime(lbWatchData.getCreateTime());
                    return ouFuData;
                }).collect(Collectors.toList());
            } else if (ReceiveDataType.BLOOD_OXYGEN.equals(type)) {
                //血氧
                list = lbWatchDataList.stream().map(lbWatchData -> {
                    OuFuData ouFuData = new OuFuData();
                    ouFuData.setBloodOxygen(lbWatchData.getBloodOxygen());
                    ouFuData.setType(ReceiveDataType.BLOOD_OXYGEN);
                    ouFuData.setCustomerId(customerId);
                    ouFuData.setCreateTime(lbWatchData.getCreateTime());
                    return ouFuData;
                }).collect(Collectors.toList());
            }

        } else if (EquipCategory.ZNSH.equals(equipmentCategory)) {
            //手环
            list = ouFuDataMapper.selectPage(customerId, type);
        }
        return BaseResult.ok(new PageInfo<>(list));
    }


    public BaseResult historySleepPage(Page page, Integer customerId, String equipmentCategory) {

         if (EquipCategory.ZNSH.equals(equipmentCategory)) {
            List<NewSleepDataView> list = ouFuService.getNewSleepDataViews(customerId);
            List<String> pageList = ListUtil.pageBySubList(list, page.getPageSize(), page.getPageNum());
            Map res = new HashMap();
            res.put("list", pageList);
            res.put("pageNum", page.getPageNum());
            res.put("pageSize", page.getPageSize());
            res.put("total", list.size());
            return BaseResult.ok(res);
        }else if (EquipCategory.LB.equals(equipmentCategory)) {
             List<OuFuData> list = new ArrayList<>();
             PageHelper.startPage(page.getPageNum(), page.getPageSize());
             List<LbWatchSleep> lbWatchSleeps = lbWatchSleepMapper.selectPageByCustomerId(customerId);
             List<NewSleepDataView> collect = lbWatchSleeps.stream().map(lbWatchSleep -> {
                 NewSleepDataView newSleepDataView = new NewSleepDataView();
                 newSleepDataView.setSleepingTime1(String.valueOf(lbWatchSleep.getDeepSleep()));
                 newSleepDataView.setSleepingTime2(String.valueOf(lbWatchSleep.getShallowSleep()));
                 newSleepDataView.setSleepingTime3(String.valueOf(lbWatchSleep.getSoberSleep()));
                 newSleepDataView.setCustomerId(customerId);
                 ZoneId zoneId = ZoneId.systemDefault();
                 Instant instant = lbWatchSleep.getCreateTime().toInstant();
                 LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
                 newSleepDataView.setCreateTime(localDateTime);
                 return newSleepDataView;
             }).collect(Collectors.toList());
             return BaseResult.ok(new PageInfo<>(collect));
         }
        return BaseResult.ok();
    }


    /*public BaseResult allHistory(Integer customerId) {
        HeartVoice heartVoice = selectLatestHeartVoiceHistory(customerId);
        Blutdruck blutdruck = (Blutdruck) selectLatestDataHistory(customerId, Blutdruck.class, blutdruckMapper);
        BloodSugar bloodSugar = (BloodSugar) selectLatestDataHistory(customerId, BloodSugar.class, bloodSugarMapper);
        if (bloodSugar != null) {
            bloodSugar.setBloodSugar(strToDouble(bloodSugar.getBloodSugar()));
        }
        StepData stepData = (StepData) selectLatestDataHistory(customerId, StepData.class, stepDataMapper);
        //SleepData sleepData = (SleepData) selectLatestDataHistory(customerId, SleepData.class, sleepDataMapper);
        NewSleepDataView newSleepDataView = sleepDataMapper.selectSleepDataViewByNewestAndCustomerId(customerId);
        HrData hrData = (HrData) selectLatestDataHistory(customerId, HrData.class, hrDataMapper);
        BpData bpData = (BpData) selectLatestDataHistory(customerId, BpData.class, bpDataMapper);
        BtData btData = (BtData) selectLatestDataHistory(customerId, BtData.class, btDataMapper);
        Customer customer = customerService.selectCustomerById(customerId);
        List<Object> list = new ArrayList<>();
        OuFuData stepOuFuData = null;
        OuFuData hrOuFuData = null;
        OuFuData bpOuFuData = null;
        OuFuData btOuFuData = null;
        if (stepData == null) {
            stepOuFuData = ouFuService.selectLastDataByType(customerId, ReceiveDataType.STEP);
        }
        if (newSleepDataView == null) {
            newSleepDataView = ouFuService.getNewSleepDataView(customerId);
            if (newSleepDataView != null) {
                newSleepDataView.setType(ReceiveDataType.SLEEP);
            }
        }
        if (hrData == null) {
            hrOuFuData = ouFuService.selectLastDataByType(customerId, ReceiveDataType.HEARTBEAT);
        }
        if (bpData == null) {
            bpOuFuData = ouFuService.selectLastDataByType(customerId, ReceiveDataType.BLOOD_PRESSURE);
        }
        if (btData == null) {
            btOuFuData = ouFuService.selectLastDataByType(customerId, ReceiveDataType.DOUBLE_TEMPERATURE);
        }

        list.add(getMap("心电仪", heartVoice));
        list.add(getMap("血压仪", blutdruck));
        list.add(getMap("血糖仪", bloodSugar));
        list.add(getMap("计步", stepData == null ? stepOuFuData : stepData));
        list.add(getMap("睡眠", newSleepDataView));
        list.add(getMap("心率", hrData == null ? hrOuFuData : hrData));
        list.add(getMap("血压", bpData == null ? bpOuFuData : bpData));
        list.add(getMap("体温", btData == null ? btOuFuData : btData));
        list.add(getMap("健康报告", customer));
        return BaseResult.ok(list);
    }*/

    public String strToDouble(String bloodSugar) {
        double doubleBloodSugar = Double.parseDouble(bloodSugar);
        doubleBloodSugar = doubleBloodSugar / 18;
        //保留一位小数点，四舍五入
        String result = String.format("%.1f", doubleBloodSugar);
        return result;
    }

    private Map getMap(String category, Object object) {
        Map map = new HashMap();
        map.put("category", category);
        map.put("data", object);
        return map;
    }

    private Map getMap(String category, String equipmentCategory, Object object) {
        Map map = new HashMap();
        map.put("category", category);
        map.put("equipmentCategory", equipmentCategory);
        map.put("data", object);
        return map;
    }

    private Object selectLatestDataHistory(Integer customerId, Class tClass, Mapper mapper) {
        Example example = new Example(tClass);
        example.orderBy("createTime").desc();
        example.createCriteria().andEqualTo("customerId", customerId);
        List<Object> datas = mapper.selectByExample(example);
        return datas.size() > 0 ? datas.get(0) : null;
    }

    /*private Blutdruck selectLatestBlutdruckHistory(Integer customerId) {
        Example example = new Example(Blutdruck.class);
        example.createCriteria().andEqualTo("customerId", customerId);
        List<Blutdruck> blutdrucks = blutdruckMapper.selectByExample(example);
        return blutdrucks.size() > 0 ? blutdrucks.get(0) : null;
    }*/

    private HeartVoice selectLatestHeartVoiceHistory(Integer customerId) {
        HeartVoiceExample example = new HeartVoiceExample();
        example.setOrderByClause("create_time desc");
        example.createCriteria().andCustomerIdEqualTo(customerId);
        List<HeartVoice> heartVoices = heartVoiceMapper.selectByExample(example);
        return heartVoices.size() > 0 ? heartVoices.get(0) : null;
    }

    public BaseResult index2(Integer customerId) throws ParseException {
        ArrayList<IndexResultDTO> results = Lists.newArrayList();
        IndexResultDTO res = IndexResultDTO.builder().build();
        //查询绑定的设备种类
        List<String> categoryList = equipmentCustomService.selectEquipmentByCustomerId(customerId).stream().map(Equipment::getCategory).collect(Collectors.toList());
        if (categoryList.size() <= 0) {
            return BaseResult.ok(Lists.newArrayList());
        }
        //来邦手表
        if (categoryList.contains(EquipCategory.LB)) {
            //健康信息
            LbWatchData lbWatchData = lbWatchDataMapper.selectLastByCustomerId(customerId);
            if (lbWatchData != null) {
                //血氧
                IndexResultDTO xy =
                        IndexResultDTO.builder().date(DateUtils.format(lbWatchData.getCreateTime(), DateUtils.MM_DD))
                                .name(AppletsConstant.BSO2)
                                .values(Lists.newArrayList(lbWatchData.getBloodOxygen().toString()))
                                .category(EquipCategory.LB).type(ReceiveDataType.BLOOD_OXYGEN).build();
                results.add(xy);
                //心率
                IndexResultDTO heart =
                        IndexResultDTO.builder().date(DateUtils.format(lbWatchData.getCreateTime(), DateUtils.MM_DD))
                                .name(AppletsConstant.XL)
                                .values(Lists.newArrayList(lbWatchData.getHeartRate().toString()))
                                .category(EquipCategory.LB).type(ReceiveDataType.HEARTBEAT).build();
                results.add(heart);
                //血压
                IndexResultDTO bp =
                        IndexResultDTO.builder().date(DateUtils.format(lbWatchData.getCreateTime(), DateUtils.MM_DD))
                                .name(AppletsConstant.XY)
                                .values(Lists.newArrayList(lbWatchData.getSystolicPressure().toString(), lbWatchData.getDiastolicPressure().toString()))
                                .category(EquipCategory.LB).type(ReceiveDataType.BLOOD_PRESSURE).build();
                results.add(bp);
            }
            //来邦步数
            LbWatchStep lbWatchStep = lbWatchStepMapper.selectLastByCustomerId(customerId);
            if (lbWatchStep != null) {
                IndexResultDTO bp =
                        IndexResultDTO.builder().date(DateUtils.format(lbWatchStep.getCreateTime(), DateUtils.MM_DD))
                                .name(AppletsConstant.JB)
                                .values(Lists.newArrayList(lbWatchStep.getStepsNumber().toString()))
                                .category(EquipCategory.LB).type(ReceiveDataType.STEP).build();
                results.add(bp);
            }
            //来邦睡眠
            LbWatchSleep lbWatchSleep = lbWatchSleepMapper.selectLastByCustomerId(customerId);
            if (lbWatchSleep != null) {
                IndexResultDTO bp =
                        IndexResultDTO.builder().date(DateUtils.format(lbWatchSleep.getCreateTime(), DateUtils.MM_DD))
                                .name(AppletsConstant.SM)
                                .values(Lists.newArrayList(String.valueOf((lbWatchSleep.getDeepSleep())),
                                        String.valueOf((lbWatchSleep.getShallowSleep())),
                                        String.valueOf((lbWatchSleep.getSoberSleep()))))
                                .category(EquipCategory.LB).type(ReceiveDataType.SLEEP).build();
                results.add(bp);
            }

        }
        //心率(心电仪，手表)
        if (categoryList.contains(EquipCategory.XDY)) {
            HeartVoice heartVoice = selectLatestHeartVoiceHistory(customerId);
            if (heartVoice != null) {
                IndexResultDTO xl =
                        IndexResultDTO.builder().date(DateUtils.format(heartVoice.getCreate_time(), DateUtils.MM_DD))
                                .name(AppletsConstant.XL)
                                .values(Lists.newArrayList(String.valueOf(heartVoice.getAvgHr())))
                                .category(EquipCategory.XDY).type(ReceiveDataType.HEARTBEAT).build();
                results.add(xl);
            }
        }
        //血压仪
        if (categoryList.contains(EquipCategory.XYY)) {
            //血压（血压仪，手表）
            Blutdruck blutdruck = (Blutdruck) selectLatestDataHistory(customerId, Blutdruck.class, blutdruckMapper);
            if (blutdruck != null) {
                IndexResultDTO xy =
                        IndexResultDTO.builder().date(blutdruck.getCreateTime().format(DateTimeFormatter.ofPattern("MM/dd")))
                                .name(AppletsConstant.XY)
                                .values(Lists.newArrayList(blutdruck.getLowData(), blutdruck.getHighData()))
                                .category(EquipCategory.XYY).type(ReceiveDataType.BLOOD_PRESSURE).build();
                results.add(xy);
            }
        }
        //血糖仪
        if (categoryList.contains(EquipCategory.XTY)) {
            BloodSugar bloodSugar = (BloodSugar) selectLatestDataHistory(customerId, BloodSugar.class, bloodSugarMapper);
            if (bloodSugar != null) {
                IndexResultDTO xt =
                        IndexResultDTO.builder().date(bloodSugar.getCreateTime().format(DateTimeFormatter.ofPattern("MM/dd")))
                                .name(AppletsConstant.XT)
                                .values(Lists.newArrayList(DataUtil.strToDouble(bloodSugar.getBloodSugar())))
                                .category(EquipCategory.XTY).type(ReceiveDataType.BLOOD_SUGAR).build();
                results.add(xt);
            }

        }
        //心晓睡眠仪
        if (categoryList.contains(EquipCategory.SMY)) {
            YouthHopeVO youthHopeVO = youthHopeService.selectNewDataByCustomerId(customerId);
            if (youthHopeVO != null) {
                String deepSleep = "";
                String lightSleep = "";
                String REMSleep = "";
                for (Report.Target target : youthHopeVO.getReport().getTarget()) {
                    if (target.getTitle().equals("深度睡眠")) {
                        deepSleep = hm2min(target.getMeasuredValue());
                    }
                    if (target.getTitle().equals("浅度睡眠")) {
                        lightSleep = hm2min(target.getMeasuredValue());
                    }
                    if (target.getTitle().equals("REM睡眠")) {
                        REMSleep = hm2min(target.getMeasuredValue());
                    }
                }
                IndexResultDTO smy =
                        IndexResultDTO.builder().date(DateUtils.format(youthHopeVO.getFinishtime(), DateUtils.MM_DD))
                                .name(AppletsConstant.SM)
                                .values(Lists.newArrayList(deepSleep, lightSleep, REMSleep))
                                .category(EquipCategory.SMY).type(ReceiveDataType.SLEEP).build();
                results.add(smy);
            }
        }
        //智能手环
        if (categoryList.contains(EquipCategory.ZNSH)) {
            //心率
            OuFuData heartbeat = ouFuService.selectLastDataByType(customerId, ReceiveDataType.HEARTBEAT, EquipCategory.ZNSH);
            if (heartbeat != null) {
                IndexResultDTO xl =
                        IndexResultDTO.builder().date(DateUtils.format(heartbeat.getCreateTime(), DateUtils.MM_DD))
                                .name(AppletsConstant.XL)
                                .values(Lists.newArrayList(String.valueOf(heartbeat.getHeartbeat())))
                                .category(EquipCategory.ZNSH).type(ReceiveDataType.HEARTBEAT).build();

                results.add(xl);
            }

            //双温
            OuFuData temperature = ouFuService.selectLastDataByType(customerId, ReceiveDataType.DOUBLE_TEMPERATURE, EquipCategory.ZNSH);
            if (temperature != null) {
                IndexResultDTO wd =
                        IndexResultDTO.builder().date(DateUtils.format(temperature.getCreateTime(), DateUtils.MM_DD))
                                .name(AppletsConstant.WD)
                                .values(Lists.newArrayList(temperature.getBodyTemperature(), temperature.getWristTemperature()))
                                .category(EquipCategory.ZNSH).type(ReceiveDataType.DOUBLE_TEMPERATURE).build();
                results.add(wd);
            }

            //步数
            OuFuData step = ouFuService.selectLastDataByType(customerId, ReceiveDataType.STEP, EquipCategory.ZNSH);
            if (step != null) {
                Integer steps = step.getSteps();
                long stepsL = Long.parseLong(steps.toString());
                String distance = RunUtils.getDistanceByStep(stepsL);
                String calorie = RunUtils.getCalorieByStep(stepsL);
                IndexResultDTO jb =
                        IndexResultDTO.builder().date(DateUtils.format(step.getCreateTime(), DateUtils.MM_DD))
                                .name(AppletsConstant.JB)
                                .values(Lists.newArrayList(String.valueOf(steps), distance, calorie))
                                .category(EquipCategory.ZNSH).type(ReceiveDataType.STEP).build();
                results.add(jb);
            }
            //如果没有睡眠仪

            //睡眠
            NewSleepDataView sleep = ouFuService.getNewSleepDataView(customerId);
            if (sleep != null) {
                IndexResultDTO sm =
                        IndexResultDTO.builder().date(DateUtils.format(DateUtils.parse(sleep.getEndTime(), DateUtils.TIME_FORMAT_2), DateUtils.MM_DD))
                                .name(AppletsConstant.SM)
                                .values(Lists.newArrayList(sleep.getSleepingTime1(), sleep.getSleepingTime2(), sleep.getSleepingTime3()))
                                .category(EquipCategory.ZNSH).type(ReceiveDataType.SLEEP).build();
                results.add(sm);
            }


            //血压
            //舍弃手表血压
            /*OuFuData bloodPressure = ouFuService.selectLastDataByType(customerId, ReceiveDataType.BLOOD_PRESSURE,EquipCategory.ZNSH);
            if (bloodPressure != null) {
                IndexResultDTO xy =
                        IndexResultDTO.builder().date(DateUtils.format(bloodPressure.getCreateTime(), DateUtils.MM_DD))
                                .name(AppletsConstant.XY)
                                .values(Lists.newArrayList(String.valueOf(bloodPressure.getDiastolic()), String.valueOf(bloodPressure.getShrink())))
                                .category(EquipCategory.ZNSH).type(ReceiveDataType.BLOOD_PRESSURE).build();
                results.add(xy);
            }*/

            //血糖不要
        } else if (categoryList.contains(EquipCategory.ZNSB)) {
            //智能手表
            //心率
            OuFuData heartbeat = ouFuService.selectLastDataByType(customerId, ReceiveDataType.HEARTBEAT, EquipCategory.ZNSB);
            if (heartbeat != null) {
                IndexResultDTO xl =
                        IndexResultDTO.builder().date(DateUtils.format(heartbeat.getCreateTime(), DateUtils.MM_DD))
                                .name(AppletsConstant.XL)
                                .values(Lists.newArrayList(String.valueOf(heartbeat.getHeartbeat())))
                                .category(EquipCategory.ZNSB).type(ReceiveDataType.HEARTBEAT).build();

                results.add(xl);
            }

            //步数
            OuFuData step = ouFuService.selectLastDataByType(customerId, ReceiveDataType.STEP, EquipCategory.ZNSB);
            if (step != null) {
                Integer steps = step.getSteps();
                long stepsL = Long.parseLong(steps.toString());
                String distance = RunUtils.getDistanceByStep(stepsL);
                String calorie = RunUtils.getCalorieByStep(stepsL);
                IndexResultDTO jb =
                        IndexResultDTO.builder().date(DateUtils.format(step.getCreateTime(), DateUtils.MM_DD))
                                .name(AppletsConstant.JB)
                                .values(Lists.newArrayList(String.valueOf(steps), distance, calorie))
                                .category(EquipCategory.ZNSB).type(ReceiveDataType.STEP).build();
                results.add(jb);
            }
            //血压
            OuFuData bloodPressure = ouFuService.selectLastDataByType(customerId, ReceiveDataType.BLOOD_PRESSURE, EquipCategory.ZNSB);
            if (bloodPressure != null) {
                IndexResultDTO xy =
                        IndexResultDTO.builder().date(DateUtils.format(bloodPressure.getCreateTime(), DateUtils.MM_DD))
                                .name(AppletsConstant.XY)
                                .values(Lists.newArrayList(String.valueOf(bloodPressure.getDiastolic()), String.valueOf(bloodPressure.getShrink())))
                                .category(EquipCategory.ZNSB).type(ReceiveDataType.BLOOD_PRESSURE).build();
                results.add(xy);
            }
        }

        //poct4
        if (categoryList.contains(EquipCategory.POCT4)) {
            //血糖
            Poct4 poctBloodSugar = poctService.selectLastPoct4DataByType(customerId, ReceiveDataType.BLOOD_SUGAR);
            if (poctBloodSugar != null) {
                IndexResultDTO xt =
                        IndexResultDTO.builder().date(DateUtils.format(poctBloodSugar.getCreateTime(), DateUtils.MM_DD))
                                .name(AppletsConstant.XT)
                                .values(Lists.newArrayList(poctBloodSugar.getValue()))
                                .category(EquipCategory.POCT4).type(ReceiveDataType.BLOOD_SUGAR).build();
                results.add(xt);
            }
            //总胆固醇
            Poct4 poctDGC = poctService.selectLastPoct4DataByType(customerId, ReceiveDataType.BILE);
            if (poctDGC != null) {
                IndexResultDTO zdgc =
                        IndexResultDTO.builder().date(DateUtils.format(poctDGC.getCreateTime(), DateUtils.MM_DD))
                                .name(AppletsConstant.ZDGC)
                                .values(Lists.newArrayList(poctDGC.getValue())).category(EquipCategory.POCT4).type(ReceiveDataType.BILE).build();
                results.add(zdgc);
            }

            //尿酸
            Poct4 poct4URICACID = poctService.selectLastPoct4DataByType(customerId, ReceiveDataType.URICACID);
            if (poct4URICACID != null) {
                IndexResultDTO ns =
                        IndexResultDTO.builder().date(DateUtils.format(poct4URICACID.getCreateTime(), DateUtils.MM_DD))
                                .name(AppletsConstant.NS)
                                .values(Lists.newArrayList(poct4URICACID.getValue())).category(EquipCategory.POCT4).type(ReceiveDataType.URICACID).build();
                results.add(ns);
            }

            //血酮
            Poct4 poct4KETONE = poctService.selectLastPoct4DataByType(customerId, ReceiveDataType.KETONE);
            if (poct4KETONE != null) {
                IndexResultDTO xtong =
                        IndexResultDTO.builder().date(DateUtils.format(poct4KETONE.getCreateTime(), DateUtils.MM_DD))
                                .name(AppletsConstant.XTONG)
                                .values(Lists.newArrayList(poct4KETONE.getValue())).category(EquipCategory.POCT4).type(ReceiveDataType.KETONE).build();
                results.add(xtong);
            }

            /**血胺 肌酐 没有*/
            //低密度蛋白
            Poct4 poct4DDENSITY = poctService.selectLastPoct4DataByType(customerId, ReceiveDataType.DDENSITY);
            if (poct4DDENSITY != null) {
                IndexResultDTO dmddb =
                        IndexResultDTO.builder().date(DateUtils.format(poct4DDENSITY.getCreateTime(), DateUtils.MM_DD))
                                .name(AppletsConstant.DMDDB)
                                .values(Lists.newArrayList(poct4DDENSITY.getValue())).category(EquipCategory.POCT4).type(ReceiveDataType.DDENSITY).build();
                results.add(dmddb);
            }

            //高密度蛋白
            Poct4 poct4UDENSITY = poctService.selectLastPoct4DataByType(customerId, ReceiveDataType.UDENSITY);
            if (poct4UDENSITY != null) {
                IndexResultDTO gmddb =
                        IndexResultDTO.builder().date(DateUtils.format(poct4UDENSITY.getCreateTime(), DateUtils.MM_DD))
                                .name(AppletsConstant.GMDDB)
                                .values(Lists.newArrayList(poct4UDENSITY.getValue())).category(EquipCategory.POCT4).type(ReceiveDataType.UDENSITY).build();
                results.add(gmddb);

            }

            //甘油三酯
            Poct4 poct4GLYCEROL = poctService.selectLastPoct4DataByType(customerId, ReceiveDataType.GLYCEROL);
            if (poct4GLYCEROL != null) {
                IndexResultDTO gysz =
                        IndexResultDTO.builder().date(DateUtils.format(poct4GLYCEROL.getCreateTime(), DateUtils.MM_DD))
                                .name(AppletsConstant.GYSZ)
                                .values(Lists.newArrayList(poct4GLYCEROL.getValue())).category(EquipCategory.POCT4).type(ReceiveDataType.GLYCEROL).build();
                results.add(gysz);
            }

        } else if (categoryList.contains(EquipCategory.POCT7)) {
            //血糖
            Poct8 poctBloodSugar = poctService.selectLastPoct8DataByType(customerId, ReceiveDataType.BLOOD_SUGAR);
            if (poctBloodSugar != null) {
                IndexResultDTO xt =
                        IndexResultDTO.builder().date(DateUtils.format(poctBloodSugar.getCreateTime(), DateUtils.MM_DD))
                                .name(AppletsConstant.XT)
                                .values(Lists.newArrayList(poctBloodSugar.getValue()))
                                .category(EquipCategory.POCT7).type(ReceiveDataType.BLOOD_SUGAR).build();
                results.add(xt);
            }

            //总胆固醇
            //Poct8 poct8 = poctService.selectLastPoct8DataByType(customerId, ReceiveDataType.BILE);
            Poct8 poctDGC = poctService.selectLastPoct8DataByType(customerId, ReceiveDataType.BILE);
            if (poctDGC != null) {
                IndexResultDTO zdgc =
                        IndexResultDTO.builder().date(DateUtils.format(poctDGC.getCreateTime(), DateUtils.MM_DD))
                                .name(AppletsConstant.ZDGC)
                                .values(Lists.newArrayList(poctDGC.getValue())).category(EquipCategory.POCT7).type(ReceiveDataType.BILE).build();
                results.add(zdgc);
            }

            //尿酸
            Poct8 poct8URICACID = poctService.selectLastPoct8DataByType(customerId, ReceiveDataType.URICACID);
            if (poct8URICACID != null) {
                IndexResultDTO ns =
                        IndexResultDTO.builder().date(DateUtils.format(poct8URICACID.getCreateTime(), DateUtils.MM_DD))
                                .name(AppletsConstant.NS)
                                .values(Lists.newArrayList(poct8URICACID.getValue())).category(EquipCategory.POCT7).type(ReceiveDataType.URICACID).build();
                results.add(ns);
            }

            //血酮
            Poct8 poct8KETONE = poctService.selectLastPoct8DataByType(customerId, ReceiveDataType.KETONE);
            if (poct8KETONE != null) {
                IndexResultDTO xtong =
                        IndexResultDTO.builder().date(DateUtils.format(poct8KETONE.getCreateTime(), DateUtils.MM_DD))
                                .name(AppletsConstant.XTONG)
                                .values(Lists.newArrayList(poct8KETONE.getValue())).category(EquipCategory.POCT7).type(ReceiveDataType.KETONE).build();
                results.add(xtong);
            }

            /**血胺 肌酐 没有*/
            //低密度蛋白
            Poct8 poct8DDENSITY = poctService.selectLastPoct8DataByType(customerId, ReceiveDataType.DDENSITY);
            if (poct8DDENSITY != null) {
                IndexResultDTO dmddb =
                        IndexResultDTO.builder().date(DateUtils.format(poct8DDENSITY.getCreateTime(), DateUtils.MM_DD))
                                .name(AppletsConstant.DMDDB)
                                .values(Lists.newArrayList(poct8DDENSITY.getValue())).category(EquipCategory.POCT7).type(ReceiveDataType.DDENSITY).build();
                results.add(dmddb);
            }

            //高密度蛋白
            Poct8 poct8UDENSITY = poctService.selectLastPoct8DataByType(customerId, ReceiveDataType.UDENSITY);
            if (poct8UDENSITY != null) {
                IndexResultDTO gmddb =
                        IndexResultDTO.builder().date(DateUtils.format(poct8UDENSITY.getCreateTime(), DateUtils.MM_DD))
                                .name(AppletsConstant.GMDDB)
                                .values(Lists.newArrayList(poct8UDENSITY.getValue())).category(EquipCategory.POCT7).type(ReceiveDataType.UDENSITY).build();
                results.add(gmddb);
            }

            //甘油三酯
            Poct8 poct8GLYCEROL = poctService.selectLastPoct8DataByType(customerId, ReceiveDataType.GLYCEROL);
            if (poct8GLYCEROL != null) {
                IndexResultDTO gysz =
                        IndexResultDTO.builder().date(DateUtils.format(poct8GLYCEROL.getCreateTime(), DateUtils.MM_DD))
                                .name(AppletsConstant.GYSZ)
                                .values(Lists.newArrayList(poct8GLYCEROL.getValue())).category(EquipCategory.POCT7).type(ReceiveDataType.GLYCEROL).build();
                results.add(gysz);
            }

        }

        return BaseResult.ok(results);
    }

    private String hm2min(String hm) {
        if ("0".equals(hm)) {
            return "0分钟";
        }
        String h = hm.split("/")[0].split("h")[0];
        String min = hm.substring(hm.indexOf("h") + 1, hm.indexOf("min"));
        Integer i = Integer.valueOf(h) * 60 + Integer.valueOf(min);
        return i.toString() + "分钟";
    }
}
